<style lang="less">
</style>

<template>
<div><Modal v-model="addmodal" :title="modaltitle" @on-ok="addadclass">
        <i-Form :model="editData" :label-width="100">
            <Form-item label="分类名称">
                <i-Input type="text" v-model="editData.title" placeholder="分类名称"></i-Input>
            </Form-item>          
            <Form-item label="分类标识">
                <i-Input type="text" v-model="editData.tag" placeholder="分类标识">
                </i-Input>
            </Form-item>       
            <Form-item label="到期时间">
                 <Date-picker type="datetime" v-model="editData.endtime" format="yyyy-MM-dd HH:mm:ss" placeholder="到期时间" style="width: 200px"></Date-picker>
            </Form-item>   
            <Form-item label="是否显示">
                <i-switch v-model="editData.show"></i-switch>
            </Form-item>   
        </i-Form>
    </Modal>
 <Card>  
        <p slot="title">
            <Icon type="navicon-round"></Icon>
            广告分类管理
        </p>       
    <Button-group>
        <Button @click="showaddmodal()">添加</Button>
        <Button @click="delduo">批量删除</Button>
    </Button-group>
    <div style="margin-top:10px"></div>
    <i-table :columns="cols" :data="data" stripe @on-selection-change="setselect" @on-sort-change="setorder" ></i-table>
    
    <div style="margin-top:10px; text-align:center">
    <Page :total="pageinfo.total" :page-size="pageinfo.pagesize" show-sizer @on-change="pagechange" @on-page-size-change="pagesizechange"></Page></div>
    </Card>
    </div>
</template>

<script>
import api from '../../libs/api';
export default {
    name: 'content_adclass',
    data () {
        return {
            addmodal:false,
            cols:[{
                    type: 'selection',
                    width: 40,
                    align: 'center'
                },{
                    title: 'ID',
                    key: 'ID',
                    width:70,
                    sortable:'custom'
                }, {
                    title: '标题',
                    key: 'title',
                    align:'left',
                    sortable:'custom',
                    render:(h,params)=>{
                        return h('Button',
                                    {
                                        props: {
                                            type: 'text',//无边框，像文字格式
                                            size: 'small',
                                        },
                                        on: {
                                            click: () => {         
                                                this.showaddmodal(Object.assign({},params.row));
                                            }
                                        }
                                    },
                                    params.row.title)
                    }
                },{
                    title: '标识',
                    key: 'tag',
                    align:'left',
                    sortable:'custom'
                },{
                    title: '状态',
                    key: 'endtime',
                    align:'center',
                    render:(h,params)=>{
                        var state="过期";
                        var color="red";
                        if(new Date(Date.parse(params.row.endtime))>new Date()&&params.row.show)
                        {
                            state="正常";
                            color="green";
                        }
                        else if(!params.row.show){
                            state="隐藏";
                            color="red";
                        }
                        return h("span",{attrs:{style:'color:'+color}},state);
                    }
                },
                {
                    title: '操作',
                    key: 'action',
                    align: 'center',
                        width:'120px',
                    render: (h, params) => {
                        return h('div',
                            [
                                h('Button',
                                    {
                                        props: {
                                            //type: 'text',//无边框，像文字格式
                                            size: 'small',
                                        },
                                        on: {
                                            click: () => {         
                                                this.showaddmodal(Object.assign({},params.row));
                                            }
                                        }
                                    },
                                    '编辑'),
                                    h('span',' '),
                                h('Button',
                                    {
                                        props: {
                                            //type: 'text',//无边框，像文字格式
                                            size: 'small',
                                        },
                                        on: {
                                            click: () => {
                                                this.deladclass(params.index);
                                            }
                                        }
                                    },
                                    '删除')
                            ])
                    }
                }],
            data:[],
            pageinfo:{page:1,pagesize:20,total:0,order:"ID",esc:"desc",key:''},
            editData:{},
            fdata:[],
            selectdata:[],
            modaltitle:"添加"
        };
    },
    methods:{
        pagesizechange(pagesize){
            this.pageinfo.pagesize=pagesize;
            this.init();
        },
        pagechange(page){
            this.pageinfo.page=page;
            this.init();
        },
        setorder(column){
            if(column.order=="normal"){
            this.pageinfo.order="ID";
            this.pageinfo.esc="desc";
            }
            else{
            this.pageinfo.order=column.key;
            this.pageinfo.esc=column.order;
            }
            this.init();
        },
        deladclass(index){
            var vm=this;            
            api.post("/adclass/del?id="+vm.data[index].ID).then(function(res){
                if(res.data.code==200){
                    vm.$Message.success(res.data.msg);
                }
                else{
                    vm.$Message.error(res.data.msg);
                }
                vm.init();
            })
        },
        setselect(selection){
            this.selectdata=selection;
        },
        delduo(){
            var vm=this; 
            api.post("/adclass/delall",{data:this.selectdata}).then(function(res){
                if(res.data.code==200){
                    vm.$Message.success("删除成功！");
                }
                vm.init();
            })
        },
        showaddmodal(model){
            this.modaltitle="编辑广告分类";
            if(model==undefined){
                var now = new Date();now.setFullYear(now.getFullYear()+10);
                this.editData={endtime:now};
            }
            else{
                this.editData=model;
            }
            this.addmodal=true;
        },
        addadclass(){
            var vm=this;            
            api.post("/adclass/save",this.editData).then(function(res){
                if(vm.editData.ID>0)
                    vm.$Message.success("修改成功！");
                else                    
                    vm.$Message.success("添加成功！");
                vm.addmodal=false;
                vm.init();
            })
        },
        init(){
            var vm=this;
            api.post("/adclass/getpagelist?t="+new Date().getSeconds(),this.pageinfo).then(function(res){
                var adclasslist=res.data.msg.Items;                
                vm.data=adclasslist;
                vm.pageinfo.total=res.data.msg.TotalItems;
            })
        }
    },
    created(){
        this.init();
    }
};
</script>
